home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Chat & Communication / Digsby build 37 / digsby_setup.exe / lib / lxml / builder.pyo (.txt) < prev    next >
Python Compiled Bytecode  |  2008-10-13  |  3KB  |  115 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. import lxml.etree as ET
  5.  
  6. try:
  7.     from functools import partial
  8. except ImportError:
  9.     
  10.     def partial(func, tag):
  11.         return (lambda : func(tag, *args, **kwargs))
  12.  
  13.  
  14.  
  15. try:
  16.     callable
  17. except NameError:
  18.     
  19.     def callable(f):
  20.         return hasattr(f, '__call__')
  21.  
  22.  
  23.  
  24. try:
  25.     basestring = __builtins__['basestring']
  26. except (NameError, KeyError):
  27.     basestring = str
  28.  
  29.  
  30. try:
  31.     unicode = __builtins__['unicode']
  32. except (NameError, KeyError):
  33.     unicode = str
  34.  
  35.  
  36. class ElementMaker(object):
  37.     
  38.     def __init__(self, typemap = None, namespace = None, nsmap = None, makeelement = None):
  39.         if namespace is not None:
  40.             self._namespace = '{' + namespace + '}'
  41.         else:
  42.             self._namespace = None
  43.         if nsmap:
  44.             self._nsmap = dict(nsmap)
  45.         else:
  46.             self._nsmap = None
  47.         if makeelement is not None:
  48.             self._makeelement = makeelement
  49.         else:
  50.             self._makeelement = ET.Element
  51.         if typemap:
  52.             typemap = typemap.copy()
  53.         else:
  54.             typemap = { }
  55.         
  56.         def add_text(elem, item):
  57.             elem.text = None if len(elem) else '' + item
  58.  
  59.         if str not in typemap:
  60.             typemap[str] = add_text
  61.         
  62.         if unicode not in typemap:
  63.             typemap[unicode] = add_text
  64.         
  65.         
  66.         def add_dict(elem, item):
  67.             attrib = elem.attrib
  68.             for k, v in item.items():
  69.                 if isinstance(v, basestring):
  70.                     attrib[k] = v
  71.                     continue
  72.                 attrib[k] = typemap[type(v)](None, v)
  73.             
  74.  
  75.         if dict not in typemap:
  76.             typemap[dict] = add_dict
  77.         
  78.         self._typemap = typemap
  79.  
  80.     
  81.     def __call__(self, tag, *children, **attrib):
  82.         get = self._typemap.get
  83.         if self._namespace is not None and tag[0] != '{':
  84.             tag = self._namespace + tag
  85.         
  86.         elem = self._makeelement(tag, nsmap = self._nsmap)
  87.         if attrib:
  88.             get(dict)(elem, attrib)
  89.         
  90.         for item in children:
  91.             if callable(item):
  92.                 item = item()
  93.             
  94.             t = get(type(item))
  95.             if t is None:
  96.                 if ET.iselement(item):
  97.                     elem.append(item)
  98.                     continue
  99.                 
  100.                 raise TypeError('bad argument type: %r' % item)
  101.                 continue
  102.             v = t(elem, item)
  103.             if v:
  104.                 get(type(v))(elem, v)
  105.                 continue
  106.         
  107.         return elem
  108.  
  109.     
  110.     def __getattr__(self, tag):
  111.         return partial(self, tag)
  112.  
  113.  
  114. E = ElementMaker()
  115.